﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameController
{
	#region 单例模型
	private static GameController singleton = new GameController();
	private GameController() { }
	public static GameController Singleton { get => singleton; }
	#endregion

	// public void GetNew();


	/// <summary>
	/// 开始游戏
	/// </summary>
	public void BeginGame() {
		// GameController();
		CameraController.Singleton.isAutoRotate = false;      // 关闭自动旋转
		enemyCount = 0;
		dieEnemyCount = 0;
		blood = 10;
		coin = 100;         // 初始数据
		UIController.Singleton.SetDieEnemyCount(dieEnemyCount);
		UIController.Singleton.SetBlood(blood);
		UIController.Singleton.SetCoin(coin);
		UIController.Singleton.SetEnemyCount(enemyCount);
	}


	/// <summary>
	/// 判断游戏是否结束
	/// </summary>
	/// <returns></returns>
	public bool JudgeGameOver() {
		if (this.blood <= 0) return true;   // 玩家失败

		// Debug.Log(EnemyController.Singleton.delNum + ":" + enemyCount);
		// 玩家胜利
		if (EnemyController.Singleton.delNum == enemyCount) return true;

		// 游戏未结束
		return false;
	}


	/// <summary>
	/// 游戏结束
	/// </summary>
	public void EndGame() {
		Debug.Log("游戏结束");
		UIController.Singleton.ShowResultPnl();   // 结束

		int num = (blood + 4) / 5;
		if (blood == 10) num++;
		UIController.Singleton.ShowStar(num);

		// if (blood <= 0) {   // 失败
		// } else if (blood <= 5) {    // 一颗星
		// } else if (blood < 10) {    // 两颗星
		// } else if (blood == 10) {   // 三颗星
		// }
	}

	/// <summary>
	/// 重新开始游戏
	/// </summary>
	public void ResetGame() {
		SceneManager.LoadScene("AssetsScene");
	}


	#region 数据管理模块
	private int dieEnemyCount;  // 消灭怪物数
	public int DieEnemyCount { get; }

	private int blood;  // 剩余血量
	public int Blood { get; }

	private int coin;   //  金币数
	public int Coin { get; }

	private int enemyCount; //怪物总数
	public int EnemyCount { get; }

	public void AddEnemyCount(int count) {
		enemyCount += count;
		UIController.Singleton.SetEnemyCount(enemyCount);
	}

	public void AddDieEnemyCount() {
		dieEnemyCount++;
		// Debug.Log(enemyCount);
		if (JudgeGameOver()) {
			EndGame();
		}
		UIController.Singleton.SetDieEnemyCount((dieEnemyCount - 10 + blood + enemyCount) % enemyCount);
	}

	public void DelBlood() {
		// blood--;
		UIController.Singleton.SetBlood(--blood);
	}

	public void AddCoin(int coin) {
		this.coin += coin;
		UIController.Singleton.SetCoin(coin);
	}
	#endregion
}
